﻿@using HCMS.Dictionary;
@model HCMS.Entities.OAuth2.OAuth2App
@{
    Layout = "~/Views/Shared/_Layout_Edit_Pear_Layui.cshtml";
}

<div class="layui-body layui-bg-gray">
    <div class="layui-card">
        <form class="layui-form">
            <input type="hidden" asp-for="Id" />

            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">基础信息</li>
                    <li>其它</li>
                </ul>

                <div class="layui-tab-content">
                    <!-- 基础信息 -->
                    <div class="layui-tab-item layui-show">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">应用 Id</div>
                                <div class="layui-form-label content-label">
                                    @Model.Id
                                </div>
                            </div>
                        }

                        <div class="layui-form-item">
                            <div class="layui-form-label">应用名称</div>
                            <div class="layui-input-inline" style="width:250px;">
                                <input type="text" asp-for="Name" lay-verify="required" autocomplete="off" placeholder="请填写名称"
                                       class="layui-input" />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">密钥</div>
                            <div class="layui-input-inline" style="width:390px;">
                                <input type="text" asp-for="Secret" lay-verify="required" autocomplete="off" placeholder="请选择 密钥"
                                       class="layui-input" readonly="readonly" />
                            </div>
                            <button type="button" class="layui-btn layui-btn-sm" lay-on="changeSecret">
                                <i class="layui-icon layui-icon-refresh-1"></i>换一个
                            </button>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">备注</div>
                            <div class="layui-input-inline" style="width:250px;">
                                <textarea type="text" asp-for="Remark" autocomplete="off" placeholder="填写具体备注说明"
                                          class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline" style="width:140px;">
                                <select asp-for="Status" asp-items="new DictCommon().StatusItems()"></select>
                            </div>
                        </div>

                    </div>

                    <!-- 其它 -->
                    <div class="layui-tab-item">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">创建时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.CreateTime @Model.CreateUser
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">更新时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.UpdateTime @Model.UpdateUser
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-fixbar btn-fixbar-box">
                <div class="layui-input-block">
                    <vc:menu-submit action-names="@(new string[]{"save"})"></vc:menu-submit>
                    <button type="button" class="layui-btn layui-btn-primary layui-border-red" lay-on="close">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@section Scripts {
    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let util = layui.util;
            var layer = layui.layer;

            submitFn();
            refreshSecret();

            // 表单提交
            function submitFn() {
                form.on('submit(save)', function (data) {

                    var layerIndex = layer.msg('正在保存', {
                      icon: 16,
                      shade: 0.1,
                      time: 0
                    });

                    $.ajax({
                        url: '/oauth2app/save/',
                        type: 'post',
                        dataType: 'json',
                        data: data.field,
                        success: function (result) {
                            if (result.code === 200) {
                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页
                                    parent.layui.table.reload("tableList", {
                                        page: { curr: 1 },
                                    });
                                });
                            } else {
                                parent.layer.msg(result.message, { icon: 2 })
                            }
                        },
                        complete‌: function(){
                            layer.close(layerIndex);
                        }
                    });

                    return false;
                });
            }

            // 更换密钥
            function refreshSecret(){

                if(parseInt($('#Id').val()) === 0){
                    ajaxFn();
                }

                util.on('lay-on', {
                    'changeSecret': function () {
                        ajaxFn();
                    }
                });

                var $Secret = $('#Secret');
                function ajaxFn(){
                    $.ajax({
                        url: '/oauth2app/getrandomsecret/',
                        type: 'get',
                        success: function (result) {
                            if (result.code === 200) {
                                $Secret.val(result.result);
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                }
            }
        });
    </script>
}